#include "StringBad.h"

#include <cstring>

using namespace std;

int StringBad::num_strings = 0;

// class methods

// constructors and others methods
StringBad::StringBad(const char* s) {
	len = strlen(s);
	str = new char[len + 1];
	strcpy_s(str, len + 1, s);
	num_strings++;
}

StringBad::StringBad(const StringBad & st)
{
	num_strings++;					// handle static member update if necessary
	len = st.len;					// same length as copied string
	str = new char[len + 1];		// alloc apce
	strcpy_s(str, len + 1, st.str);	// copy string to new location
}

StringBad::StringBad() {
	len = 0;
	str = new char[1];
	str[0] = '\0';
	num_strings++;
}

StringBad::~StringBad() {
	--num_strings;
	delete[] str;
}


// overloaded operator methods
StringBad & StringBad::operator=(const StringBad & st)	// copy assignment
{
	if (this == &st) {				// object assigned to itself
		return *this;				// all done
	}
	delete[] str;					// free old string space
	len = st.len;					// same length as copied string
	str = new char[len + 1];		// alloc apce
	strcpy_s(str, len + 1, st.str);	// copy string to new location
	return *this;					// return reference to invoking object
}

StringBad & StringBad::operator=(StringBad&& st)		// move assignment 
{ 
	if (this == &st) {				// object assigned to itself
		return *this;				// all done
	}
	delete[] str;					// free old string space
	len = st.len;					// same length as copied string
	str = st.str;					// modify the pointer to this
	st.len = 0;						// reset old
	st.str = nullptr;				// reset old
	return *this;					// return reference to invoking object
}

StringBad & StringBad::operator=(const char * c)
{
	delete[] str; // free old string space
	len = strlen(c);
	str = new char[len + 1];
	strcpy_s(str, len + 1, c);
	return *this;
}

char & StringBad::operator[](int i)
{
	// read-write char access for non-const StringBad
	return str[i];
}

const char & StringBad::operator[](int i) const
{
	// read-only char access for const StringBad
	return str[i];
}

// overloaded operator friends
bool operator<(const StringBad & st1, const StringBad & st2)
{
	return (strcmp(st1.str, st2.str) < 0);
}

bool operator>(const StringBad & st1, const StringBad & st2)
{
	return st2 < st1;
}

bool operator==(const StringBad & st1, const StringBad & st2)
{
	return (strcmp(st1.str, st2.str) == 0);
}

ostream & operator<<(ostream& os, const StringBad& st) {
	os << st.str;
	return os;
}

istream & operator>>(istream & is, StringBad & st)
{
	char temp[StringBad::CINLIM];
	is.get(temp, StringBad::CINLIM);
	if (is) {
		st = temp;
	}
	while (is && is.get() != '\n')
	{
		continue;
	}
	return is;
}

int StringBad::HowMany()
{
	return 0;
}